package com.shop2cn.iapi.agentproducts.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.AddConditionRebateBean;
import com.shop2cn.iapi.agentproducts.parameter.EnableConditionRebateBean;
import com.shop2cn.iapi.agentproducts.service.EnableConditionRebateCall;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebate;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateExample;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValue;
import com.ymttest.database.model.agentrebate.Ymt_ConditionRebateValueExample;
import com.ymttest.database.model.godmch.mch_depot_info;
/**
*启用/禁用返佣条件Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_EnableConditionRebate{
	private static EnableConditionRebateBean enableconditionrebateBean;
	private static EnableConditionRebateCall enableconditionrebateCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("启用/禁用返佣条件Test");
	}

	@Before
	public void caseUp() {
		enableconditionrebateBean=new EnableConditionRebateBean();
		enableconditionrebateCall=new EnableConditionRebateCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(enableconditionrebateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EnableConditionRebate_001() {
		Logger.start(true,"禁用一个返佣条件");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			createDepotBean bean=GodmchManageCallService.createDepot(userId);
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(2000);
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			Thread.sleep(2000);
			
			enableconditionrebateBean.setUserId(userId);
			enableconditionrebateBean.setConditionRebateId(RebateId);
			enableconditionrebateBean.setEnable(false);
			enableconditionrebateCall.setData(enableconditionrebateBean);
			enableconditionrebateCall.callService();			
			Logger.verifyEquals("200", enableconditionrebateCall.getString("code"), "验证code");
			
			checkConditionRebate(RebateId,false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EnableConditionRebate_002() {
		Logger.start(true,"禁用，然后启用一个返佣条件");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			createDepotBean bean=GodmchManageCallService.createDepot(userId);
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(2000);
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			Thread.sleep(2000);
			
			enableconditionrebateBean.setUserId(userId);
			enableconditionrebateBean.setConditionRebateId(RebateId);
			enableconditionrebateBean.setEnable(false);
			enableconditionrebateCall.setData(enableconditionrebateBean);
			enableconditionrebateCall.callService();			
			Logger.verifyEquals("200", enableconditionrebateCall.getString("code"), "验证code");
			
			checkConditionRebate(RebateId,false);
			
			
			enableconditionrebateBean.setEnable(true);
			enableconditionrebateCall.setData(enableconditionrebateBean);
			enableconditionrebateCall.callService();			
			Logger.verifyEquals("200", enableconditionrebateCall.getString("code"), "验证code");
			
			checkConditionRebate(RebateId,true);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_EnableConditionRebate_003() {
		Logger.start(true,"禁用多次，还是禁用");
		try {
			Integer userId = GodmchManageCallService.CreateAllMsgSellerMch().getMchId();
			createDepotBean bean=GodmchManageCallService.createDepot(userId);
			List<mch_depot_info> selectDepotByDepotTitle = GodmchManageCallService.SelectDepotByDepotTitle(bean.getDepotTitle());
			// 给渠道商创建一个商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(userId, "", true);
			addAgentProductBean.setDepotId(selectDepotByDepotTitle.get(0).getDepotId());
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Thread.sleep(2000);
			AddConditionRebateBean initAddCondictionRebate = AddAgentProductCallService.InitAddCondictionRebate(addAgentProductBean);
			int RebateId = AddAgentProductCallService.AddCondictionRebate(initAddCondictionRebate);
			Thread.sleep(2000);
			
			enableconditionrebateBean.setUserId(userId);
			enableconditionrebateBean.setConditionRebateId(RebateId);
			enableconditionrebateBean.setEnable(false);
			enableconditionrebateCall.setData(enableconditionrebateBean);
			enableconditionrebateCall.callService();			
			enableconditionrebateCall.callService();			
			enableconditionrebateCall.callService();			
			Logger.verifyEquals("200", enableconditionrebateCall.getString("code"), "验证code");
			
			checkConditionRebate(RebateId,false);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	/**
	 * 验证数据库是否被禁用
	 * @param b 
	 * @param bean
	 */
	private void checkConditionRebate(int rebateId, boolean b){
		Ymt_ConditionRebateExample example2=new Ymt_ConditionRebateExample();
		example2.createCriteria()
		.andConditionRebateIdEqualTo(rebateId);
		Ymt_ConditionRebate ymt_ConditionRebate = SqlMapperFactory.getIntergratedProductMapper().selectYmtConditionRebateExample(example2).get(0);
		
		Logger.verifyEquals(b?0:-2,ymt_ConditionRebate.getAction(), "验证已经被删除");
	}
	
	
}